import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const usePermissionStore = defineStore('permission', () => {
  // 状态
  const userButtons = ref([])
  const userMenus = ref([])
  const userInfo = ref(null)
  
  // 刷新权限的回调函数
  const refreshCallback = ref(null)

  // 计算属性
  const hasPermission = computed(() => {
    return (permission) => {
      if (!permission) return true
      return userButtons.value.includes(permission)
    }
  })

  const hasAnyPermission = computed(() => {
    return (permissions) => {
      if (!permissions || permissions.length === 0) return true
      return permissions.some(permission => userButtons.value.includes(permission))
    }
  })

  const hasAllPermissions = computed(() => {
    return (permissions) => {
      if (!permissions || permissions.length === 0) return true
      return permissions.every(permission => userButtons.value.includes(permission))
    }
  })

  // 方法
  const setUserPermissions = (buttons, menus, info) => {
    userButtons.value = buttons || []
    userMenus.value = menus || []
    userInfo.value = info || null
    
    // 同时更新全局变量，保持兼容性
    window.userButtons = userButtons.value
  }

  const getUserPermissions = () => {
    return userButtons.value
  }

  const getUserMenus = () => {
    return userMenus.value
  }

  const getUserInfo = () => {
    return userInfo.value
  }

  const clearPermissions = () => {
    userButtons.value = []
    userMenus.value = []
    userInfo.value = null
    window.userButtons = []
  }

  const setRefreshCallback = (callback) => {
    refreshCallback.value = callback
    window.refreshUserPermissions = callback
  }

  const refreshPermissions = async () => {
    if (refreshCallback.value) {
      await refreshCallback.value()
      return true
    }
    return false
  }

  return {
    // 状态
    userButtons,
    userMenus,
    userInfo,
    
    // 计算属性
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    
    // 方法
    setUserPermissions,
    getUserPermissions,
    getUserMenus,
    getUserInfo,
    clearPermissions,
    setRefreshCallback,
    refreshPermissions
  }
})

// 为了保持向后兼容性，导出一些函数
export const usePermissions = () => {
  const store = usePermissionStore()
  return {
    userButtons: computed(() => store.userButtons),
    userMenus: computed(() => store.userMenus),
    userInfo: computed(() => store.userInfo),
    hasPermission: store.hasPermission,
    hasAnyPermission: store.hasAnyPermission,
    hasAllPermissions: store.hasAllPermissions,
    getUserPermissions: store.getUserPermissions,
    getUserMenus: store.getUserMenus,
    getUserInfo: store.getUserInfo,
    refreshPermissions: store.refreshPermissions
  }
}

// 导出一些全局函数以保持兼容性
export const setUserPermissions = (buttons, menus, info) => {
  const store = usePermissionStore()
  store.setUserPermissions(buttons, menus, info)
}

export const hasPermission = (permission) => {
  const store = usePermissionStore()
  return store.hasPermission.value(permission)
}

export const clearPermissions = () => {
  const store = usePermissionStore()
  store.clearPermissions()
}

export const setRefreshCallback = (callback) => {
  const store = usePermissionStore()
  store.setRefreshCallback(callback)
}

export const refreshPermissions = async () => {
  const store = usePermissionStore()
  return await store.refreshPermissions()
} 